package com.didi.gulimall.product.controller;

import com.didi.gulimall.common.utils.PageUtils;
import com.didi.gulimall.common.utils.R;
import com.didi.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.didi.gulimall.product.entity.AttrEntity;
import com.didi.gulimall.product.entity.AttrGroupEntity;
import com.didi.gulimall.product.service.AttrGroupService;
import com.didi.gulimall.product.service.AttrService;
import com.didi.gulimall.product.service.CategoryService;
import com.didi.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author didi
 * @email 1062664904@qq.com
 * @date 2021-01-14 15:42:45
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;

    /**
     * 获取分类下所有分组&关联属性
     * @param catelogId
     * @return
     */
    @GetMapping("{catelogId}/withattr")
    public R withattrByCateLogId(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> attrsVos = attrService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",attrsVos);
    }
    @GetMapping("{attrgroupId}/noattr/relation")
    public R attrNoRelation(
            @RequestParam Map<String, Object> params,
            @PathVariable("attrgroupId") Long attrgroupId) {
        PageUtils page = attrService.attrNoRelation(params, attrgroupId);
        return R.ok().put("page", page);
    }

    @DeleteMapping("attr/relation/delete")
    public R deleteAttrRelation(@RequestBody List<AttrAttrgroupRelationEntity> attrGroupWithAttrsVos) {
        attrService.deleteAttrRelation(attrGroupWithAttrsVos);
        return R.ok();
    }

    @PostMapping("attr/relation")
    public R saveAttrRelation(@RequestBody List<AttrAttrgroupRelationEntity> attrGroupWithAttrs) {
        attrService.saveAttrRelation(attrGroupWithAttrs);
        return R.ok();
    }

    @GetMapping("{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", entities);
    }

    /**
     * 列表
     */
    @GetMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        if (attrGroup != null) {
            List<Long> cateLogPath = categoryService.findCateLogPath(attrGroup.getCatelogId());
            attrGroup.setCateLogPath(cateLogPath);
        }
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
